package memorphic.simpleloginsample.controllers
{
	import memorphic.simpleloginsample.ISampleAppContext;
	import memorphic.simpleloginsample.SampleStateManager;
	import memorphic.simpleloginsample.events.LoginEvent;
	import memorphic.simpleloginsample.model.LoginStatus;
	
	
	/**
	 * ROLE: Controller
	 * 	The responsibilities of a Controller are to:
	 * 		- deal with user interaction events from the view (the view or associated Presentation Model 
	 * 			will call methods like submitLogin(), as a result of user interaction, to accomplish this)
	 * 		- update the Model
	 * 		- deal with business delegates
	 * 		- update application state (via the context's state manager)
	 * 		
	 */
	public class LoginController
	{

		
		
		private var context:ISampleAppContext;
		
		
		private var loginDelegate:LoginDelegate;


		public function LoginController(context:ISampleAppContext)
		{
			this.context = context;
			loginDelegate = new LoginDelegate();
			loginDelegate.addEventListener(LoginEvent.LOG_IN, onLoginOrRegisterSuccess);
			loginDelegate.addEventListener(LoginEvent.LOG_OUT, onLoginOrRegisterSuccess);
			loginDelegate.addEventListener(LoginEvent.LOG_IN_ERROR, onLoginOrRegisterError);
			loginDelegate.addEventListener(LoginEvent.REGISTER, onLoginOrRegisterSuccess);
			loginDelegate.addEventListener(LoginEvent.REGISTER_ERROR, onLoginOrRegisterError);
		}
		
		
		
		public function submitLogin(name:String, pass:String):void
		{
			context.stateManager.stateURI = SampleStateManager.LOGIN;
			
			context.session.error = "";
			context.session.userName = name;
			context.session.password = pass;
			context.session.status = LoginStatus.AUTHENTICATING;
			
			loginDelegate.submitLogin(name, pass);
		}
		
		
		public function submitRegistration(name:String, pass:String, pass2:String):void
		{
			context.session.userName = name;
			context.session.status = LoginStatus.REGISTERING;
			loginDelegate.submitRegistration(name, pass, pass2);
		}
		
		
		public function showLogin():void
		{
			context.stateManager.stateURI = SampleStateManager.LOGIN;
		}
		
		public function showRegistration():void
		{
			context.stateManager.stateURI = SampleStateManager.REGISTER;
		}
		
		public function logOut():void
		{
			loginDelegate.logOut();
		}
		
		
		
		private function onLoginOrRegisterError(e:LoginEvent):void
		{
			context.session.error = e.error;
			context.session.status = LoginStatus.LOGGED_OUT;
			if(e.type == LoginEvent.LOG_IN_ERROR){
				context.stateManager.stateURI = SampleStateManager.LOGIN;			
			}else if(e.type == LoginEvent.REGISTER_ERROR){
				context.stateManager.stateURI = SampleStateManager.REGISTER;			
			}
		}
		
		private function onLoginOrRegisterSuccess(e:LoginEvent):void
		{
			context.session.error = "";
			switch(e.type){
			case LoginEvent.LOG_IN:
				context.session.status = LoginStatus.LOGGED_IN;
				context.stateManager.goHome();
				break;
			case LoginEvent.REGISTER:
			case LoginEvent.LOG_OUT:
				context.session.status = LoginStatus.LOGGED_OUT;
				context.stateManager.stateURI = SampleStateManager.LOGIN;
				break;
			}
		}

	}
}


